package com.lucky.common.config;

import com.lucky.common.interceptor.AjaxInterceptor;
import com.lucky.common.interceptor.LoggerInterceptor;
import com.lucky.common.interceptor.SessionInterceptor;
import com.lucky.common.interceptor.TokenInterceptor;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zhangboqing
 * @date 2019-07-28
 *
 * MVC配置类
 */
@Configuration
public class MvcConfigurer implements WebMvcConfigurer {

	@Value("${app.img.path")
	String filePath;

	@Resource
	AjaxInterceptor ajaxInterceptor;
	@Resource
	LoggerInterceptor loggerInterceptor;
	@Resource
	SessionInterceptor sessionInterceptor;
	@Resource
	TokenInterceptor tokenInterceptor;

	/**
	 * 拦截器配置
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		String[] includePatterns = {"/**"};

		registry.addInterceptor(tokenInterceptor)
				.addPathPatterns(includePatterns)
				.excludePathPatterns(AppConfig.ignoreUrls);
		registry.addInterceptor(sessionInterceptor)
				.addPathPatterns(includePatterns)
				.excludePathPatterns(AppConfig.ignoreUrls);

	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		//注意如果filePath是写死在这里，一定不要忘记尾部的/或者\\，这样才能读取其目录下的文件
		registry.addResourceHandler("/app/img/**").addResourceLocations("file:"+filePath);

	}

	/**
	 * 拦截器配置
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {

		registry.addViewController("/").setViewName("index");
		registry.addViewController("/index.html").setViewName("index");
		registry.addViewController("/main.html").setViewName("main");

	}



}

